package pub.wkq.auth.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import pub.wkq.auth.mapper.AdminMapper;
import pub.wkq.auth.service.AdminService;
import org.springframework.stereotype.Service;
import pub.wkq.bo.AdminBO;
import pub.wkq.common.utils.MD5Utils;
import pub.wkq.pojo.Admin;

import javax.annotation.Resource;

/**
 * <p>
 * 慕聘网运营管理系统的admin账户表，仅登录，不提供注册 服务实现类
 * </p>
 *
 * @author 王开琦
 * @since 2023-06-05
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Override
    public Boolean adminLogin(AdminBO adminBO) {
        //1. 根据用户名获得盐 salt
        Admin admin = getSelfAdmin(adminBO.getUsername());
        if (admin == null) {
            return false;
        }
        String salt = admin.getSlat();
        String md5Str = MD5Utils.encrypt(adminBO.getPassword(), salt);
        return md5Str.equalsIgnoreCase(admin.getPassword());
    }

    @Override
    public Admin getAdminInfo(AdminBO adminBO) {
        return getSelfAdmin(adminBO.getUsername());
    }

    private Admin getSelfAdmin(String username) {
        Admin admin = new QueryChainWrapper<>(adminMapper).eq("username",username).one();
        return admin;
    }

}
